/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author thien_000
 */
public class bean_khachhang {

    private String MaKH;
    private String TaiKhoan;
    private String MatKhau;
    private String HoTen;
    private String GioiTinh;
    private String Email;
    private String SoDT;

    public bean_khachhang() {

    }

    public bean_khachhang(String makh, String taikhoan, String matkhau, String hoten, String gioitinh, String email, String sodt) {
        this.Email = email;
        this.GioiTinh = gioitinh;
        this.HoTen = hoten;
        this.MaKH = makh;
        this.MatKhau = matkhau;
        this.SoDT = sodt;
        this.TaiKhoan = taikhoan;
    }

    public bean_khachhang(String taikhoan, String matkhau, String hoten, String gioitinh, String email, String sodt) {
        this.Email = email;
        this.GioiTinh = gioitinh;
        this.HoTen = hoten;
        this.MatKhau = matkhau;
        this.SoDT = sodt;
        this.TaiKhoan = taikhoan;
    }

    /**
     * @return the MaKH
     */
    public String getMaKH() {
        return MaKH;
    }

    /**
     * @return the TaiKhoan
     */
    public String getTaiKhoan() {
        return TaiKhoan;
    }

    /**
     * @return the MatKhau
     */
    public String getMatKhau() {
        return MatKhau;
    }

    /**
     * @return the HoTen
     */
    public String getHoTen() {
        return HoTen;
    }

    /**
     * @return the GioiTinh
     */
    public String getGioiTinh() {
        return GioiTinh;
    }

    /**
     * @return the Email
     */
    public String getEmail() {
        return Email;
    }

    /**
     * @return the SoDT
     */
    public String getSoDT() {
        return SoDT;
    }

    /**
     * @param MaKH the MaKH to set
     */
    public void setMaKH(String MaKH) {
        this.MaKH = MaKH;
    }

    /**
     * @param TaiKhoan the TaiKhoan to set
     */
    public void setTaiKhoan(String TaiKhoan) {
        this.TaiKhoan = TaiKhoan;
    }

    /**
     * @param MatKhau the MatKhau to set
     */
    public void setMatKhau(String MatKhau) {
        this.MatKhau = MatKhau;
    }

    /**
     * @param HoTen the HoTen to set
     */
    public void setHoTen(String HoTen) {
        this.HoTen = HoTen;
    }

    /**
     * @param GioiTinh the GioiTinh to set
     */
    public void setGioiTinh(String GioiTinh) {
        this.GioiTinh = GioiTinh;
    }

    /**
     * @param Email the Email to set
     */
    public void setEmail(String Email) {
        this.Email = Email;
    }

    /**
     * @param SoDT the SoDT to set
     */
    public void setSoDT(String SoDT) {
        this.SoDT = SoDT;
    }

    public bean_khachhang[] hienthikhachhang() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM khachhang";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();
            while (rs.next()) {
                String makh = rs.getString("MaKH");
                String taikhoan = rs.getString("TaiKhoan");
                String matkhau = rs.getString("MatKhau");
                String hoten = rs.getString("HoTen");
                String gioitinh = rs.getString("GioiTinh");
                String email = rs.getString("Email");
                String sodt = rs.getString("SoDT");
                bean_khachhang nv = new bean_khachhang(makh, taikhoan, matkhau, hoten, gioitinh, email, sodt);
                list.add(nv);
            }
            bean_khachhang[] result = new bean_khachhang[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public bean_khachhang[] searchLikeHoTen(String HoTen) {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = Connect.GetConnect();
            String sql = "SELECT * FROM khachhang WHERE HoTen LIKE ?";
            stm = con.prepareStatement(sql);
            stm.setString(1, "%" + HoTen + "%");
            rs = stm.executeQuery();
            ArrayList list = new ArrayList();

            while (rs.next()) {
                String makh = rs.getString("MaKH");
                String taikhoan = rs.getString("TaiKhoan");
                String matkhau = rs.getString("MatKhau");
                String hoten = rs.getString("HoTen");
                String gioitinh = rs.getString("GioiTinh");
                String email = rs.getString("Email");
                String sodt = rs.getString("SoDT");
                bean_khachhang nv = new bean_khachhang(makh, taikhoan, matkhau, hoten, gioitinh, email, sodt);
                list.add(nv);
            }
            bean_khachhang[] result = new bean_khachhang[list.size()];
            list.toArray(result);
            rs.close();
            stm.close();
            con.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insert(String makh, String taikhoan, String matkhau,
            String hoten, String gioitinh, String email, String sodt) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "INSERT INTO khachhang(makh,taikhoan,matkhau,hoten,gioitinh,email,sodt)" + "VALUES(?,?,?,?,?,?,?)";
            stm = con.prepareStatement(sql);
            stm.setString(1, makh);
            stm.setString(2, taikhoan);
            stm.setString(3, matkhau);
            stm.setString(4, hoten);
            stm.setString(5, gioitinh);
            stm.setString(6, email);
            stm.setString(7, sodt);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteKhachHang() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM khachhang WHERE TaiKhoan=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, TaiKhoan);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateKhachHang() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "UPDATE khachhang SET MatKhau=?,HoTen=?,GioiTinh=?,Email=?,SoDT=? WHERE TaiKhoan=?";
            stm = con.prepareStatement(sql);
            stm.setString(1, MatKhau);
            stm.setString(2, HoTen);
            stm.setString(3, GioiTinh);
            stm.setString(4, Email);
            stm.setString(5, SoDT);
            stm.setString(6, TaiKhoan);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean deleteAll() {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = Connect.GetConnect();
            String sql = "DELETE FROM khachhang";
            stm = con.prepareStatement(sql);
            int result = stm.executeUpdate();
            stm.close();
            con.close();
            if (result > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
